<?php
/**
 * 用作个人中心
 */
namespace Home\Controller;

use Home\Model\AdminUserModel;
use Think\Controller;
use Common\Controller\UBaseController;

class IndexController extends UBaseController
{
    /**
     * 个人信息
     */
    public function index()
    {
        //配置当前页面
        $this->assign("pageId", "A001001");
        //设置页面标题,非必须
        $this->_title("个人中心");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('index');
    }

    /**
     * 重置密码
     */
    public function resetpwd()
    {
        //配置当前页面
        $this->assign("pageId", "A001002");
        //设置页面标题,非必须
        $this->_title("重置密码");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('resetpwd');
    }

    public function listp()
    {
        $p = $this->params['p'] ?: 1;
        $limit = 10;
        $offset = ($p - 1) * $limit;

        if($this->params['status'] == 'no') {
            $con = [
                'status' => 0
            ];
        } else {
            $con = [
                'status' => 1
            ];
        }

        $order = [
            'id' => 'DESC'
        ];
        $adminModel = new AdminUserModel();
        $list = $adminModel->where($con)->order($order)->limit($offset, $limit)->select();
        foreach ($list as $k=>$v) {
            unset($list[$k]['password']);
            $list[$k]['created'] = date("Y-m-d", $v['created']);
        }
        $this->quickBuildForm();
        $this->assign("list", $list);
        //配置当前页面
        $this->assign("pageId", "A001002");
        //设置页面标题,非必须
        $this->_title("管理员列表");
        //页面公共模板调用
        layout("default");
        //选取数据模板
        $this->display('list');
    }

    /**
     * ajax请求
     */
    public function ajax()
    {
        if (empty($this->params['action'])) {
            $this->jsonReturn(10000,"","参数不全");
        }

        switch ($this->params['action']) {
            case 'checkPass':
                $pass = $this->params['password'];

                $adminModel = new AdminUserModel();
                $res = $adminModel->checkPassword($this->user['id'], $pass);

                if ($res['status']) {
                    $this->jsonReturn(200,"","密码正确");
                } else {
                    $this->jsonReturn($res['code'],"", $res['msg']);
                }

                break;

            case 'resetPass':
                $pass = $this->params['password'];
                $new  = $this->params['newpass'];
                $repeat = $this->params['repeatpass'];

                if (empty($pass) || empty($new) || empty($repeat)) {
                    $this->jsonReturn(10000,"","参数不全");
                }

                if ($new !== $repeat) {
                    $this->jsonReturn(10000,"", "两次新密码不同!");
                }

                $adminModel = new AdminUserModel();
                $res = $adminModel->checkPassword($this->user['id'], $pass);
                if (!$res['status']) {
                    $this->jsonReturn($res['code'],"", $res['msg']);
                }

                $res = $adminModel->newPassword($this->user['id'], $new);

                if ($res['status']) {
                    $this->jsonReturn(200,"","密码更新成功!");
                } else {
                    $this->jsonReturn($res['code'], "", $res['msg']);
                }
                break;
            default :
                $this->jsonReturn();
                break;
        }

        exit;

    }
    public function ttt()
    {
        echo "ttt";exit;
//        $pwd = md5('silov@237');
//        $param = [
//            'username'  => 'silov',
//            'password'  => $pwd,
//            'nickname'  => '洋葱'
//        ];
//
//        $sign = md5( http_build_query($param) . md5('PaRaMsKeY!@#$&^%') );
//
//        echo $pwd."<br>".$sign;
    }

}